import 'package:flutter_common_fragement/common/base/event_bus/index.dart';

class EventBusUtil {
  ///  广播实例
  static EventBus? _eventBus;

  /// 工具单例
  static EventBusUtil? _instance;

  /// 命名构造函数 初始化操作
  EventBusUtil._internal() {
    _eventBus = EventBus();

    /// 初始化实例
  }

  /// 工厂函数 构造单例
  factory EventBusUtil() {
    _instance ??= EventBusUtil._internal();
    return _instance!;
  }

  /// 信号发出
  void fire(event) {
    _eventBus?.fire(event);
  }

  /// 监听
  void listen<T>(void Function(T) callback) {
    _eventBus?.on<T>().listen((event) {
      callback(event);
    });
  }

  /// 销毁
  void destroy() {
    if (_eventBus == null) return;
    _eventBus?.destroy();

    /// 保证下次使用EventBusUtil().fire() 的时候存在eventBus单例
    _eventBus = null;
    _instance = null;
  }
}
